package com.example.demo.runner.code;

import java.io.InputStream;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;

public class StreamReader implements Runnable {
    private final Scanner scanner;
    private final BlockingQueue<RunnerMessage> queue;

    public StreamReader(InputStream stream, BlockingQueue<RunnerMessage> queue) {
        this.scanner = new Scanner(stream);
        this.queue = queue;
    }

    @Override
    public void run() {
        while (scanner.hasNext()) {
            try {
                String next = scanner.next();
                queue.put(new RunnerMessage(next));
            } catch (InterruptedException e) {
                // TODO check exception
                e.printStackTrace();
                break;
            }
        }
        System.out.println("code reader stop");
    }
}
